HTMLify
Stock span problem.java
Views: 1 | Author: cody
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 | // Stock span problem import java.util.*; import java.io.*; import java.lang.*; class gfg { public static void main(String args[]) { Scanner sc = new Scanner(System.in); int t = sc.nextInt(); while(t-- > 0) { int n =sc.nextInt(); int a[] = new int[n]; int i = 0; for(i = 0; i < n; i++) a[i] = sc.nextInt(); int[] s = new Solution().calculateSpan(a, n); for(i = 0; i < n; i++) { System.out.print(s[i] + " "); } System.out.println(); } } } // } Driver Code Ends class Solution { //Function to calculate the span of stock’s price for all n days. public static int[] calculateSpan(int price[], int n) { // Your code here int [] ans = new int[n]; Stack<Integer> st = new Stack<>(); for(int i = n-1; i>=0; i--){ while(st.size()>0 && price[i] > price[st.peek()]){ int idx = st.pop(); ans[idx] = idx-i; } st.push(i); } while(st.size()>0){ int idx = st.pop(); ans[idx] = idx+1; } return ans; } } |